#include "shadowmessagebox.h"

ShadowMessageBox::ShadowMessageBox(QWidget *parent)
    :QWidget(parent)
    ,autoAddOkButton(true)
{
    init();
}

ShadowMessageBox::ShadowMessageBox(const QString &title, const QString &text, ShadowMessageBox::Icon icon,
                                   StandardButton button1, StandardButton button2, StandardButton button3,  QWidget *parent)
    :QWidget(parent)
    ,autoAddOkButton(true)
{
    init(title, text);
    setIcon(icon);
    addButtons(button1, button2, button3);
}


ShadowMessageBox::StandardButton ShadowMessageBox::information(QWidget *parent,const QString &title,
                                                               const QString &text, StandardButton button1,
                                                               StandardButton button2, StandardButton button3)
{
    ShadowMessageBox msgBox(title, text,ShadowMessageBox::Icon::Information,
                            button1, button2, button3, parent);

    msgBox.show();
    if (msgBox.exec() == -1) {
        return ShadowMessageBox::Cancel;
    }

    return msgBox.clickedButton();
}

ShadowMessageBox::StandardButton ShadowMessageBox::critical(QWidget *parent, const QString &title,
                                                            const QString &text, StandardButton button1,
                                                            StandardButton button2, StandardButton button3)
{
    ShadowMessageBox msgBox(title, text, ShadowMessageBox::Icon::Critical,
                            button1, button2, button3, parent);

    msgBox.show();
    if (msgBox.exec() == -1) {
        return ShadowMessageBox::Cancel;
    }

    return msgBox.clickedButton();
}

// TODO 此处修改标题未起效
void ShadowMessageBox::setTitle(const QString &title)
{
    m_widg->setWindowTitle(title);
}

void ShadowMessageBox::setText(const QString &text)
{
    m_msgText->setText(text);
}

void ShadowMessageBox::setIcon(Icon icon)
{
    QString iconPath;
    switch (icon) {
    case Icon::Information:
        iconPath = ":/images/images/TS.png";
        break;
    case Icon::Critical:
        iconPath = ":/images/images/CG.png";
        break;
    default:
        break;
    }
    m_msgIcon->setPixmap(QPixmap(iconPath));
}

void ShadowMessageBox::addButton(StandardButton button)
{
    autoAddOkButton = false;
    QString buttonText;
    switch (button) {
    case StandardButton::NoButton:
        return;
    case StandardButton::Ok:
        buttonText = "OK";
        break;
    case StandardButton::Yes:
        buttonText = "Yes";
        break;
    case StandardButton::No:
        buttonText = "No";
        break;
    default:
        return;
    }
    QPushButton* pushButton = new QPushButton(buttonText, this);
    pushButton->setMinimumWidth(60);
    m_buttonHash.insert(pushButton, newButton(button));
    m_hbtnLayout->addWidget(pushButton);
    pushButton->setStyleSheet("border:1px solid rgba(0,140,215,1);font: 14px \"Microsoft YaHei\";color: white");
    connect(pushButton, &QPushButton::clicked,
            this, &ShadowMessageBox::setClickedButton);
    connect(pushButton, &QPushButton::clicked, this, &QWidget::close);
}

void ShadowMessageBox::addButton(const QString &text)
{
    autoAddOkButton = false;
    QPushButton* pushButton = new QPushButton(text, this);
    pushButton->setMinimumWidth(60);
    m_btnGroup->addButton(pushButton, m_btnGroup->buttons().size());
    m_hbtnLayout->addWidget(pushButton);
    pushButton->setStyleSheet("border:1px solid rgba(0,140,215,1);font: 14px \"Microsoft YaHei\";color: white");
//    connect(pushButton, &QPushButton::clicked,
//            this, &ShadowMessageBox::setClickedButton);
    connect(pushButton, &QPushButton::clicked, this, &QWidget::close);
}

void ShadowMessageBox::addButton(QAbstractButton *button)
{
    autoAddOkButton = false;
//    m_buttonHash.insert(pushButton, newButton(button));
    m_btnGroup->addButton(button, m_btnGroup->buttons().size());
    m_hbtnLayout->addWidget(button);
    button->setStyleSheet("border:1px solid rgba(0,140,215,1);font: 14px \"Microsoft YaHei\";color: white");
//    connect(pushButton, &QPushButton::clicked,
//            this, &ShadowMessageBox::setClickedButton);
    connect(button, &QPushButton::clicked, this, &QWidget::close);
}

int ShadowMessageBox::exec()
{
    return eventLoop->exec();
}

ShadowMessageBox::StandardButton ShadowMessageBox::newButton(int button)
{
    StandardButton sbtn;
    switch (button) {
    case StandardButton::Ok:
        sbtn = StandardButton::Ok;
        break;
    case StandardButton::Yes:
        sbtn = StandardButton::Yes;
        break;
    case StandardButton::No:
        sbtn = StandardButton::No;
        break;
    default:
        sbtn = StandardButton::NoButton;
        break;
    }

    return sbtn;
}

void ShadowMessageBox::addButtons(StandardButton button1, StandardButton button2, StandardButton button3)
{
    addButton(newButton(button1));
    addButton(newButton(button2));
    addButton(newButton(button3));
}

void ShadowMessageBox::init(const QString &title, const QString &text)
{
    this->setAttribute(Qt::WA_TranslucentBackground);
//    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowFlags(Qt::FramelessWindowHint |Qt::Dialog);
    this->setWindowModality(Qt::WindowModal);

    m_btnGroup = new QButtonGroup(this);
    connect(m_btnGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
            [=] (QAbstractButton* btn) {
                eventLoop->exit();
                emit signal_buttonClicked(btn);
            });

    this->setMinimumSize(210, 150);
    m_widg = new QWidget(this);

    m_pTitleBar = new TitleBar(m_widg);
    m_widg->installEventFilter(m_pTitleBar);
    m_widg->setWindowTitle(title);
    m_widg->setWindowIcon(QIcon(":/images/images/MV_BJ_AbnormalScreening_icon.ico"));

    QPalette pal(palette());
    pal.setColor(QPalette::Background, QColor(0,87,152,255));
    m_widg->setAutoFillBackground(true);
    m_widg->setPalette(pal);
    m_pLayout = new QVBoxLayout(m_widg);
    m_pLayout->addWidget(m_pTitleBar);
    m_pLayout->setSpacing(0);
    m_pLayout->setMargin(0);
    m_widg->setLayout(m_pLayout);

    m_msgIcon  = new QLabel(m_widg);
    m_msgIcon->setFixedSize(32,32);
    m_msgIcon->setPixmap(QPixmap());
    m_msgIcon->setScaledContents(true);

    m_msgText = new QLabel(m_widg);
    m_msgText->setStyleSheet("font: 14px \"Microsoft YaHei\";color: rgba(254,254,255,1);");
    m_msgText->setText(text);

    m_hlabLayout = new QHBoxLayout();
    m_hlabLayout->addWidget(m_msgIcon);
    m_hlabLayout->addWidget(m_msgText);
    m_hlabLayout->setSpacing(5);
    m_hlabLayout->setContentsMargins(5,0,5,0);

    m_pLayout->addLayout(m_hlabLayout);

    m_btnWidg = new QWidget(m_widg);
    QPalette pal2(palette());
    pal2.setColor(QPalette::Background, QColor(0,61,121,255));
    m_btnWidg->setAutoFillBackground(true);
    m_btnWidg->setPalette(pal2);

    m_hbtnLayout= new QHBoxLayout(m_btnWidg);
    m_hbtnLayout->setMargin(6);
    m_hbtnLayout->setSpacing(6);
    QSpacerItem* leftBtnSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding);
    m_hbtnLayout->addItem(leftBtnSpacer);

    m_pLayout->addWidget(m_btnWidg);

    m_hpLayout = new QHBoxLayout(this);
    m_hpLayout->addWidget(m_widg);
    m_hpLayout->setContentsMargins(20, 20, 20, 20);

    m_pEffect = new QGraphicsDropShadowEffect(m_widg);
    m_pEffect->setOffset(0, 0);
    m_pEffect->setColor(QColor(QStringLiteral("black")));
    m_pEffect->setBlurRadius(30);
    m_widg->setGraphicsEffect(m_pEffect);

    eventLoop = new QEventLoop(this);
}

void ShadowMessageBox::showEvent(QShowEvent *event)
{
    if (autoAddOkButton) {
        addButton(Ok);
    }

    QWidget::showEvent(event);
}

void ShadowMessageBox::setClickedButton()
{
    QPushButton* button = static_cast<QPushButton*>(sender());
    m_clickedButton = button;
}

ShadowMessageBox::StandardButton ShadowMessageBox::clickedButton()
{
    eventLoop->exit();
    return m_buttonHash.value(m_clickedButton);
}
